package com.yunzheng.studyLeaveSystem.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBUtils {
    private static String driverClass;
    private static String url;
    private static String username;
    private static String password;

    static {
        InputStream in = DBUtils.class.getClassLoader().getResourceAsStream("mysql.properties");
        Properties properties = new Properties();
        try {
            properties.load(in);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        driverClass = properties.getProperty("driverClass");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
        try {
            in.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getCon() {
        try {
            Connection con = DriverManager.getConnection(url, username, password);
            return con;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static void release(ResultSet rs, PreparedStatement ps,Connection con) throws Exception{
        if (rs != null){
            rs.close();
        }
        if (ps != null){
            ps.close();
        }
        if (con != null){
            con.close();
        }
    }
}
